<?php

class Application_Model_BaseModel
{
    protected  $dbTableName = '';
    protected $values = array();
    
	public function setOptions(array $options)
	{
	    $methods = get_class_methods($this);
	    foreach ($options as $key => $value) {
	        $method = 'set_' . $key;
	        if (in_array($method, $methods)) {
	            $this->$method($value);
	        }
	    }
	    return $this;
	}
    
    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }

    /*public function __set($name, $value)
    {
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid property');
        }
        $this->$method($value);
    }

    public function __get($name)
    {
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid property');
        }
        return $this->$method();
    }*/
    
    public function set($name, $value){
        $dbTable = $this->dbTableName;
        if ($name == null || !in_array($name, $dbTable::getRowNames()))
            throw new Exception('Invalid property: ' . $name);
        $this->values[$name] = $value;
        return $this;
    }
    
    public function get($name){
        $dbTable = $this->dbTableName;
        if (!in_array($name, $dbTable::getRowNames()))
            throw new Exception('Invalid property: ' . $name);
        if (!isset($this->values[$name])){
            return null;
        } else {
            return $this->values[$name];
        }
    }
}

